/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.script;

import tal.drivechain.parsing.Tokenizer;

/**
 *
 * @author Main
 */
public class ScriptCommand {
    //[name] [param1] [param2]
    private String commandName, arg1, arg2;

    public ScriptCommand(String commandName, String arg1, String arg2) {
        this.commandName = commandName;
        this.arg1 = arg1;
        this.arg2 = arg2;
    }
    
    public static ScriptCommand parseScriptCommand(String command) {
        command = command.trim().replaceAll("\\s+", " ");
        Tokenizer tokenizer = new Tokenizer(command, ' ');
        if (tokenizer.countTokens()==1) 
            return new ScriptCommand(command, "", "");
        else if (tokenizer.countTokens()==2) {
            return new ScriptCommand(tokenizer.getTokens()[0], tokenizer.getTokens()[1], "");
        } else {
            String commandName = tokenizer.getTokens()[0];
            String arg1 = tokenizer.getTokens()[1];
            String arg2 = tokenizer.getTokens()[2];
            for (int i=3; i<tokenizer.countTokens(); i++)
                arg2 += " " + tokenizer.getTokens()[i];
            return new ScriptCommand(commandName, arg1.trim(), arg2.trim());
        }
    }
    
    @Override
    public String toString() {
        return commandName + " " + arg1 + " " + arg2;
    }

    public int getArgCount() {
        int i = 0;
        if (commandName.trim().length()>0) i++;
        if (arg1.trim().length()>0) i++;
        if (arg2.trim().length()>0) i++;
        return i;
    }

    public String getName() { return commandName; }
    public String getArg1() { return arg1; }
    public String getArg2() { return arg2; }
}
